#include "BPHbasicdraw.h"

BPHbasicdraw::BPHbasicdraw()
{

}

 int BPHbasicdraw::drawBPH(  QPainter &painter,QByteArray myb,int myddx,int myddy,int withrectangle ,int withcap ,int BPHdirect,int kk,QColor myc,QColor mybc7,QColor mybc6,QColor mybc5,QColor mybc4,QColor mybc3,QColor mybc2,QColor mybc1,QColor mybc0,QList<int> LF,bool mustDraw)
{ ///BPH ������ʼ��
    //��С�ߴ�
  int bitgap = 1;//10;
  int H = 8;//80;
  int W = 8;//80;
  int dH =2;//20;hope dH=2/8*H��dH�� ��bit line �϶� ���� topline �Ŀհ� �߶� ��Ҳ�ǳ�bit line �¶˾���bottomline�Ŀհ׸߶ȡ�
  int dHs =3;//20;hope dHs=3/8*H��dHs�� ��bit line �϶� ���� topline �Ŀհ� �߶� ��Ҳ�Ƕ�bit line �¶˾���bottomline�Ŀհ׸߶ȡ�
     //end ��С�ߴ�

  //int kk=2;//5;//5;�Ŵ�����   ��ʱ��֧�� ������   ��

  int ddx=0;//��ʼ ��ĳ��BPH�����Ͻ� �ο��� ���� ��ddx ,ddy��
  int ddy=0;//��ʼ ��ĳ��BPH�����Ͻ� �ο��� ���� ��ddx ,ddy��
    // ����������ɫΪ��ɫ,���� ��  ��Χ     �� LF   �� capñ   ��
  QPen pen= QPen(myc, 1);
  painter.setPen(QPen(myc, 1));
    //����
    bitgap=bitgap*kk;
    H=H*kk;
    W=W*kk;
    dH=dH*kk;
    dHs=dHs*kk;
      //end ����

  // end BPH ������ʼ��

    //�õ�һ�� BPHռ�õĳߴ�
    int BPHw=0;
    int BPHh=0;//��ʱû�� �������� BPHw
    if  (withrectangle==1) { //�� �� �߽� �� ���ȴ�Щ  ��
       BPHw=(W + bitgap * 2 + bitgap);  //x ������  ����  BPHw
    }
    else if (withrectangle==0) {//�����߽磬
       BPHw=W;  //x����      ���� BPHw
    }
    BPHh=BPHw;
      // end �õ�һ�� BPHռ�õĳߴ�


    if (mustDraw==true) {
    //many BPH , �����ֽ�����  ,��
    for(int ii = 0; ii < myb.size(); ii ++) {
              //���� �ο��㣨ddx ,ddy����
               /*
              if  (withrectangle==1) { //�� �� �߽� �� ���ȴ�Щ  ��
               ddx=myddx+ii*(W + bitgap * 2 + bitgap);  //x ������  ���� BPHw
               ddy=myddy;
              }
              else if (withrectangle==0) {//�����߽磬
                  ddx=myddx+ii*W;  //x����
                  ddy=myddy;
              }
               */
             ddx=myddx+ii*BPHw;  //x ������  ���� BPHw
             ddy=myddy; //y ����  ����ʱ����Ҫ
               ///

          // BPH  ,cap to up ,big memory index bit to left��BMIB_left��
          //	BPHdirect==0 or 1, ���߽��� �Ӿ��ı��� ���ˡ�
     if (BPHdirect==0)
         {
               ///

              //�� �� �߽� �� �����ձ߽� border
              if  (withrectangle==1) {
               int decy =1;// 5;//'5Ŀ�� ��  W>H, opencv cv2.������ת�������鷳
                       //	 ''border top
                        painter.drawLine(ddx, ddy+decy, ddx+W+bitgap*2, ddy+decy);
                       //	 ''border bottom line
                        painter.drawLine(ddx, ddy+H+bitgap*2, ddx+W+bitgap*2, ddy+H+bitgap*2);
                       //	 'border left line
                        painter.drawLine(ddx, ddy+decy, ddx, ddy+H+bitgap*2);
                       //	 'border right line
                        painter.drawLine(ddx+W+bitgap*2, ddy+decy, ddx+W+bitgap*2, ddy+H+bitgap*2);
              }

              //�� �� �߽� �������߽��� ������ ���� �ο��㣨ddx ,ddy����
              if  (withrectangle==1) {
               ddx=ddx+bitgap;
               ddy=ddy+bitgap;
                        }

              /////draw a BHP:

               //
                 if  (withcap==1) {
               //top line(cap),width=W-bitgap/2  ��(-bitgap/2,Ŀ�� �� ��ֹ ���� BPH��һƬ ���������� )
               painter.drawLine(ddx, ddy, ddx+W-bitgap/2, ddy);
                 }

               //bottom line,width=W-bitgap/2��(-bitgap/2,Ŀ�� �� ��ֹ ���� BPH��һƬ ���������� )
               painter.drawLine(ddx, ddy+H, ddx+W-bitgap/2, ddy+H);
               //short line , at corner,height=dH/2
               painter.drawLine(ddx,ddy+H-dH/2, ddx, ddy+H);
               //LF
               if (LF[ii]==2) {
                  //short line , at corner,height=dH/2
                   painter.drawLine(ddx+bitgap,ddy+H-dH/2, ddx+bitgap, ddy+H);
               }

               //8 bit line (�߳�  ��ͼ�Ǵ��У����ǲ����������� ʱ�������ϵı��ؼ���LE С��,�Ȼ���line0,���󻭵�line7��)��Ҳ�ɱ����ñ��� ����BE, �Ȼ�line7 ��������line0��
               for(int i = 7; i  >=0; i --) {
                  //if (a>= Math.pow(2,i))
                   //���� ��������ɫ
                   if (i==7) {pen.setColor(mybc7);}
                    else if(i==6) {pen.setColor(mybc6);}
                    else if(i==5) {pen.setColor(mybc5);}
                    else if(i==4) {pen.setColor(mybc4);}
                    else if(i==3) {pen.setColor(mybc3);}
                    else if(i==2) {pen.setColor(mybc2);}
                    else if(i==1) {pen.setColor(mybc1);}
                    else if(i==0) {pen.setColor(mybc0);}
                   //
                   painter.setPen(pen);

                   if (((myb[ii]>>i)&0x01)==1)
                  {
                    // become tall bit line ,height=H-2dH
                    painter.drawLine(ddx+(7-i)*bitgap,ddy+dH, ddx+(7-i)*bitgap, ddy+H-dH);
                    //a= (a-(int)(Math.pow(2,i)));
                  } else {

                   // become  short bit line,height<=H/8 ,( hope<=H/8)
                    painter.drawLine(ddx+(7-i)*bitgap,ddy+dHs, ddx+(7-i)*bitgap, ddy+H-dHs);
                  }
               }

             /////end a drawBPH
     }

     //BPH to right  ,cap to right ,big memory index bit to up��BMIB_up��
     else if(BPHdirect==1){

             //�� �� �߽� �� �����ձ߽� border
             if  (withrectangle==1) {
              int decy =1;// 5;//'5Ŀ�� ��  W>H, opencv cv2.������ת�������鷳
                   //	 ''border top
                    painter.drawLine(ddx, ddy+decy, ddx+W+bitgap*2, ddy+decy);
                   //	 ''border bottom line
                    painter.drawLine(ddx, ddy+H+bitgap*2, ddx+W+bitgap*2, ddy+H+bitgap*2);
                   //	 'border left line
                    painter.drawLine(ddx, ddy+decy, ddx, ddy+H+bitgap*2);
                   //	 'border right line
                    painter.drawLine(ddx+W+bitgap*2, ddy+decy, ddx+W+bitgap*2, ddy+H+bitgap*2);
             }

             //�� �� �߽� �������߽��� ������ ���� �ο��㣨ddx ,ddy����
             if  (withrectangle==1) {
              ddx=ddx+bitgap;
              ddy=ddy+bitgap;
                    }

             /////draw a BHP:

              //
                if  (withcap==1) {
              //top line(cap),width=W-bitgap/2  ��(-bitgap/2,Ŀ�� �� ��ֹ ���� BPH��һƬ ���������� )
              painter.drawLine(ddx+W-bitgap/2, ddy, ddx+W-bitgap/2, ddy+H);
                }

              //bottom line,width=W-bitgap/2��(-bitgap/2,Ŀ�� �� ��ֹ ���� BPH��һƬ ���������� )
              painter.drawLine(ddx, ddy, ddx, ddy+H);
              //short line , at corner,height=dH/2
              painter.drawLine(ddx,ddy, ddx+dH/2, ddy);
              //LF
              if (LF[ii]==2) {
                  //short line , at corner,height=dH/2
                  painter.drawLine(ddx,ddy+bitgap, ddx+dH/2, ddy+bitgap);
              }

              //8 bit line
              for(int i = 7; i  >=0; i --) {
               //if (a>= Math.pow(2,i))
                   //���� ��������ɫ
               if (i==7) {pen.setColor(mybc7);}
                else if(i==6) {pen.setColor(mybc6);}
                    else if(i==5) {pen.setColor(mybc5);}
                    else if(i==4) {pen.setColor(mybc4);}
                    else if(i==3) {pen.setColor(mybc3);}
                    else if(i==2) {pen.setColor(mybc2);}
                    else if(i==1) {pen.setColor(mybc1);}
                    else if(i==0) {pen.setColor(mybc0);}
               //
                painter.setPen(pen);


                  if (((myb[ii]>>i)&0x01)==1)
              {
                   // become tall bit line ,height=H-2dH
                painter.drawLine(ddx+dH,ddy+(7-i)*bitgap,ddx+W-dH, ddy+(7-i)*bitgap);
                  // a=a-(int) Math.pow(2,i);
              } else {

               // become  short bit line,height<=H/8 ,( hope<=H/8)
                  painter.drawLine(ddx+dHs,ddy+(7-i)*bitgap,ddx+W-dHs, ddy+(7-i)*bitgap);
                  //g.drawLine(ddx+(7-i)*bitgap,ddy+H/2-H/16, ddx+(7-i)*bitgap, ddy+H/2+H/16);
                  }
              }

            /////end a drawBPH
    }
     //	BPHdirect==2 or 3, ���߽��� �Ӿ��ı��� С�ˡ�
     //BPH to down  ,cap to down ,big memory index bit to right��BMIB_right��:
     else if(BPHdirect==2){
         ///

         //�� �� �߽� �� �����ձ߽� border
         if  (withrectangle==1) {
          int decy =1;// 5;//'5Ŀ�� ��  W>H, opencv cv2.������ת�������鷳
               //	 ''border top
                painter.drawLine(ddx, ddy+decy, ddx+W+bitgap*2, ddy+decy);
               //	 ''border bottom line
                painter.drawLine(ddx, ddy+H+bitgap*2, ddx+W+bitgap*2, ddy+H+bitgap*2);
               //	 'border left line
                painter.drawLine(ddx, ddy+decy, ddx, ddy+H+bitgap*2);
               //	 'border right line
                painter.drawLine(ddx+W+bitgap*2, ddy+decy, ddx+W+bitgap*2, ddy+H+bitgap*2);
         }

         //�� �� �߽� �������߽��� ������ ���� �ο��㣨ddx ,ddy����
         if  (withrectangle==1) {
          ddx=ddx+bitgap;
          ddy=ddy+bitgap;
                }

         /////draw a BHP:

          //
            if  (withcap==1) {
          //top line(cap),width=W-bitgap/2  ��(-bitgap/2,Ŀ�� �� ��ֹ ���� BPH��һƬ ���������� )
          painter.drawLine(ddx, ddy+H, ddx+W-bitgap/2, ddy+H);
            }

          //bottom line,width=W-bitgap/2��(-bitgap/2,Ŀ�� �� ��ֹ ���� BPH��һƬ ���������� )
          painter.drawLine(ddx, ddy, ddx+W-bitgap, ddy);
          //short line , at corner,height=dH/2
          painter.drawLine(ddx+W-bitgap,ddy+dH/2, ddx+W-bitgap, ddy);
          //LF
          if (LF[ii]==2) {
          //short line , at corner,height=dH/2
              painter.drawLine(ddx+W-2*bitgap,ddy+dH/2, ddx+W-2*bitgap, ddy);
          }

          //8 bit line (�߳�  ��ͼ�Ǵ��У����ǲ����������� ʱ�������ϵı��ؼ���LE С��,�Ȼ���line0,���󻭵�line7��)��Ҳ�ɱ����ñ��� ����BE, �Ȼ�line7 ��������line0��
          for(int i = 7; i  >=0; i --) {
          //if (a>= Math.pow(2,i))
           //���� ��������ɫ
           if (i==7) {pen.setColor(mybc7);}
            else if(i==6) {pen.setColor(mybc6);}
                else if(i==5) {pen.setColor(mybc5);}
                else if(i==4) {pen.setColor(mybc4);}
                else if(i==3) {pen.setColor(mybc3);}
                else if(i==2) {pen.setColor(mybc2);}
                else if(i==1) {pen.setColor(mybc1);}
                else if(i==0) {pen.setColor(mybc0);}
           //
            painter.setPen(pen);

           if (((myb[ii]>>i)&0x01)==1)
          {
               // become tall bit line ,height=H-2dH
            painter.drawLine(ddx+(i)*bitgap,ddy+dH, ddx+(i)*bitgap, ddy+H-dH);
               //a= (a-(int)(Math.pow(2,i)));
          } else {

           // become  short bit line,height<=H/8 ,( hope<=H/8)
                painter.drawLine(ddx+(i)*bitgap,ddy+dHs, ddx+(i)*bitgap, ddy+H-dHs);
              }
          }

        /////end a drawBPH

     }
     //BPH to left  ,cap to left ,big memory index bit to down��BMIB_down��:
     else if(BPHdirect==3){

         //�� �� �߽� �� �����ձ߽� border
         if  (withrectangle==1) {
          int decy =1;// 5;//'5Ŀ�� ��  W>H, opencv cv2.������ת�������鷳
               //	 ''border top
                painter.drawLine(ddx, ddy+decy, ddx+W+bitgap*2, ddy+decy);
               //	 ''border bottom line
                painter.drawLine(ddx, ddy+H+bitgap*2, ddx+W+bitgap*2, ddy+H+bitgap*2);
               //	 'border left line
                painter.drawLine(ddx, ddy+decy, ddx, ddy+H+bitgap*2);
               //	 'border right line
                painter.drawLine(ddx+W+bitgap*2, ddy+decy, ddx+W+bitgap*2, ddy+H+bitgap*2);
         }

         //�� �� �߽� �������߽��� ������ ���� �ο��㣨ddx ,ddy����
         if  (withrectangle==1) {
          ddx=ddx+bitgap;
          ddy=ddy+bitgap;
                }

         /////draw a BHP:

          //
            if  (withcap==1) {
          //top line(cap),width=W-bitgap/2  ��(-bitgap/2,Ŀ�� �� ��ֹ ���� BPH��һƬ ���������� )
          painter.drawLine(ddx+bitgap, ddy, ddx+bitgap, ddy+H-bitgap);
            }

          //bottom line,width=W-bitgap/2��(-bitgap/2,Ŀ�� �� ��ֹ ���� BPH��һƬ ���������� )
          painter.drawLine(ddx+W, ddy, ddx+W, ddy+H-bitgap);
          //short line , at corner,height=dH/2
          painter.drawLine(ddx+W,ddy+H-bitgap, ddx+W-dH/2, ddy+H-bitgap);
          //LF
          if (LF[ii]==2) {
              //short line , at corner,height=dH/2
              painter.drawLine(ddx+W,ddy+H-2*bitgap, ddx+W-dH/2, ddy+H-2*bitgap);
          }

          //8 bit line
          for(int i = 7; i  >=0; i --) {
           //if (a>= Math.pow(2,i))
               //���� ��������ɫ
           if (i==7) {pen.setColor(mybc7);}
            else if(i==6) {pen.setColor(mybc6);}
                else if(i==5) {pen.setColor(mybc5);}
                else if(i==4) {pen.setColor(mybc4);}
                else if(i==3) {pen.setColor(mybc3);}
                else if(i==2) {pen.setColor(mybc2);}
                else if(i==1) {pen.setColor(mybc1);}
                else if(i==0) {pen.setColor(mybc0);}
           //
           painter.setPen(pen);


              if (((myb[ii]>>i)&0x01)==1)
          {
               // become tall bit line ,height=H-2dH
            painter.drawLine(ddx+dH,ddy+(i)*bitgap,ddx+W-dH, ddy+(i)*bitgap);
              // a=a-(int) Math.pow(2,i);
          } else {

           // become  short bit line,height<=H/8 ,( hope<=H/8)
              painter.drawLine(ddx+dHs,ddy+(i)*bitgap,ddx+W-dHs, ddy+(i)*bitgap);
              //g.drawLine(ddx+(7-i)*bitgap,ddy+H/2-H/16, ddx+(7-i)*bitgap, ddy+H/2+H/16);
              }
          }

        /////end a drawBPH


     }
   }//end for...�ֽ����� ���� , many BPH ,�����ֽ�����

   }//if mustDraw==true

    //���� �ܿ��ȡ�������Ҫ
    int ii =  myb.size();
    int totalBPHwidth=0;
       /*
      if  (withrectangle==1) { //�� �� �߽� �� ���ȴ�Щ  ��
         totalBPHwidth=ii*(W + bitgap * 2 + bitgap);
        }
      else if (withrectangle==0) {//�����߽磬
           totalBPHwidth=ii*W;
        }
        */
    totalBPHwidth=ii*BPHw;
   return totalBPHwidth;





}
